{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/\n",
      "Collecting py2neo\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c7/76/8615d736888db5793cef66165ca45807188c41031bfbc2533f84eab74e20/py2neo-2021.2.3-py2.py3-none-any.whl (177 kB)\n",
      "Collecting interchange~=2021.0.4Note: you may need to restart the kernel to use updated packages.\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/88/bd/abc58e5a36a28e0e55501f4bc15df74e430f399375b14b83f4ce22a257b4/interchange-2021.0.4-py2.py3-none-any.whl (28 kB)\n",
      "Collecting pansi>=2020.7.3\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0b/15/7972e08b7ec14a8b10d5ff206c644d4478906c909c134123ed7e6bd16724/pansi-2020.7.3-py2.py3-none-any.whl (10 kB)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -ip (d:\\python3.8.6\\lib\\site-packages)\n",
      "WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.\n",
      "You should consider upgrading via the 'D:\\python3.8.6\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Requirement already satisfied: pygments>=2.0.0 in c:\\users\\huawei\\appdata\\roaming\\python\\python38\\site-packages (from py2neo) (2.7.1)\n",
      "Requirement already satisfied: urllib3 in d:\\python3.8.6\\lib\\site-packages (from py2neo) (1.26.6)\n",
      "Collecting monotonic\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9a/67/7e8406a29b6c45be7af7740456f7f37025f0506ae2e05fb9009a53946860/monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n",
      "Requirement already satisfied: packaging in c:\\users\\huawei\\appdata\\roaming\\python\\python38\\site-packages (from py2neo) (20.4)\n",
      "Requirement already satisfied: six>=1.15.0 in c:\\users\\huawei\\appdata\\roaming\\python\\python38\\site-packages (from py2neo) (1.15.0)\n",
      "Requirement already satisfied: certifi in d:\\python3.8.6\\lib\\site-packages (from py2neo) (2021.5.30)\n",
      "Requirement already satisfied: pytz in d:\\python3.8.6\\lib\\site-packages (from interchange~=2021.0.4->py2neo) (2021.3)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in c:\\users\\huawei\\appdata\\roaming\\python\\python38\\site-packages (from packaging->py2neo) (2.4.7)\n",
      "Installing collected packages: pansi, monotonic, interchange, py2neo\n",
      "Successfully installed interchange-2021.0.4 monotonic-1.6 pansi-2020.7.3 py2neo-2021.2.3\n"
     ]
    }
   ],
   "source": [
    "pip install py2neo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import py2neo\n",
    "from py2neo import Graph,NodeMatcher,Node,Relationship"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "g=Graph(\"http://localhost:7474\",user='neo4j',password='123')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df=pd.read_csv(\"triples_file_inverse.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>head</th>\n",
       "      <th>label</th>\n",
       "      <th>tail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>467ecb30-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>hasConnectivity</td>\n",
       "      <td>467ecdce-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>467ecb30-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>hasVulnerability</td>\n",
       "      <td>467edb0c-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>467ecb30-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>hasPrivilege</td>\n",
       "      <td>467ed1b6-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>467ecb30-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>hasPrivilege</td>\n",
       "      <td>467ed238-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>467ecb30-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>hasPrivilege</td>\n",
       "      <td>467ed29c-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54837</th>\n",
       "      <td>510d481a-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>inverse_hasOS</td>\n",
       "      <td>510d4720-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54838</th>\n",
       "      <td>512f9ca8-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>inverse_hasOS</td>\n",
       "      <td>512f9bcc-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54839</th>\n",
       "      <td>5163778a-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>inverse_hasOS</td>\n",
       "      <td>516376a4-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54840</th>\n",
       "      <td>51161ae4-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>inverse_hasOS</td>\n",
       "      <td>511619fe-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54841</th>\n",
       "      <td>51282ea0-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "      <td>inverse_hasOS</td>\n",
       "      <td>51282d9c-bf2d-11ec-8f5a-174fcd6b0f73</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>54842 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       head             label  \\\n",
       "0      467ecb30-bf2d-11ec-8f5a-174fcd6b0f73   hasConnectivity   \n",
       "1      467ecb30-bf2d-11ec-8f5a-174fcd6b0f73  hasVulnerability   \n",
       "2      467ecb30-bf2d-11ec-8f5a-174fcd6b0f73      hasPrivilege   \n",
       "3      467ecb30-bf2d-11ec-8f5a-174fcd6b0f73      hasPrivilege   \n",
       "4      467ecb30-bf2d-11ec-8f5a-174fcd6b0f73      hasPrivilege   \n",
       "...                                     ...               ...   \n",
       "54837  510d481a-bf2d-11ec-8f5a-174fcd6b0f73     inverse_hasOS   \n",
       "54838  512f9ca8-bf2d-11ec-8f5a-174fcd6b0f73     inverse_hasOS   \n",
       "54839  5163778a-bf2d-11ec-8f5a-174fcd6b0f73     inverse_hasOS   \n",
       "54840  51161ae4-bf2d-11ec-8f5a-174fcd6b0f73     inverse_hasOS   \n",
       "54841  51282ea0-bf2d-11ec-8f5a-174fcd6b0f73     inverse_hasOS   \n",
       "\n",
       "                                       tail  \n",
       "0      467ecdce-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "1      467edb0c-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "2      467ed1b6-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "3      467ed238-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "4      467ed29c-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "...                                     ...  \n",
       "54837  510d4720-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "54838  512f9bcc-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "54839  516376a4-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "54840  511619fe-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "54841  51282d9c-bf2d-11ec-8f5a-174fcd6b0f73  \n",
       "\n",
       "[54842 rows x 3 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "54842it [35:41, 25.61it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "for i,row in tqdm(df.iterrows()):\n",
    "    start_node=NodeMatcher(g).match('漏洞',name=row['head']).first()\n",
    "    end_node=NodeMatcher(g).match('漏洞',name=row['tail']).first()\n",
    "    if start_node is None:\n",
    "        start_node=Node('漏洞',name=row['head'])\n",
    "        g.create(start_node)\n",
    "    if end_node is None:\n",
    "        end_node=Node('漏洞',name=row['tail'])\n",
    "        g.create(end_node)\n",
    "    relation=Relationship(start_node,row['label'],end_node)\n",
    "    g.create(relation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><tr><th>r</th><th>k</th></tr><tr><td style=\"text-align:left\">{}</td><td style=\"text-align:left\">{name: &#039;\\u96ea\\u96c1&#039;}</td></tr><tr><td style=\"text-align:left\">{}</td><td style=\"text-align:left\">{name: &#039;\\u7d2b\\u9e4a&#039;}</td></tr><tr><td style=\"text-align:left\">{}</td><td style=\"text-align:left\">{name: &#039;\\u6625\\u7ea4&#039;}</td></tr></table>"
      ],
      "text/plain": [
       " r                    | k                                    \n",
       "----------------------|--------------------------------------\n",
       " (雪雁)-[:丫鬟 {}]->(林黛玉) | (_126:Person {name: '\\u96ea\\u96c1'}) \n",
       " (紫鹊)-[:丫鬟 {}]->(林黛玉) | (_129:Person {name: '\\u7d2b\\u9e4a'}) \n",
       " (春纤)-[:丫鬟 {}]->(林黛玉) | (_130:Person {name: '\\u6625\\u7ea4'}) "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.run(\"match(n:Person {name:'林黛玉'})-[r:丫鬟]-(k)   return r,k\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7852afdaf9e7274afa032eaec216d523779e02e5753aa07ac78ccb4bbaf816cb"
  },
  "kernelspec": {
   "display_name": "Python 3.8.6rc1 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
